﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms.DataVisualization.Charting;

using RexToy.UI.WinForm.Controls;
using RexToy.UI.WinForm.Pmlx;

namespace MGAS.UI.Controls
{
    public class HistoryChart : UserControl, ITemplateItem
    {
        private const string AREA = "area";

        public Color LineColor { get; set; }
        public Color GridColor { get; set; }
        public double InitialValue { get; set; }
        public bool AxisXLabel { get; set; }
        public bool AxisYLabel { get; set; }

        [PmlxBoundControl("chart")]
        protected Chart _chart;

        private ChartArea _area;
        private Series _s;
        public HistoryChart()
        {
            _area = new ChartArea(AREA);

            _s = new Series();
            _s.ChartType = SeriesChartType.FastLine;

            _chart.ChartAreas.Add(_area);
            _chart.Series.Add(_s);
            _s.ChartArea = AREA;

            _chart.BackGradientStyle = System.Windows.Forms.DataVisualization.Charting.GradientStyle.TopBottom;
            _chart.BackSecondaryColor = System.Drawing.Color.White;

            _chart.BorderlineColor = System.Drawing.Color.FromArgb(26, 59, 105);
            _chart.BorderlineDashStyle = System.Windows.Forms.DataVisualization.Charting.ChartDashStyle.Solid;
            _chart.BackColor = Color.SteelBlue;
        }

        public void UpdateData(int val)
        {
            if (this.Disposing || this.IsDisposed)
                return;

            if (_s.Points.Count == 60)
                _s.Points.RemoveAt(0);
            _s.Points.AddY(val);

            if (_area.AxisY.Maximum < val)
                _area.AxisY.Maximum = val * 1.1;
        }

        public void ParseTemplate(PmlxNode template)
        {
            _area.AxisX.LabelStyle.Enabled = AxisXLabel;
            _area.AxisY.LabelStyle.Enabled = AxisYLabel;

            _area.AxisX.LineColor = GridColor;
            _area.AxisX.MajorGrid.LineColor = GridColor;
            _area.AxisY.LineColor = GridColor;
            _area.AxisY.MajorGrid.LineColor = GridColor;

            _s.Color = LineColor;

            for (var i = 0; i < 60; i++)
            {
                _s.Points.AddY(InitialValue);
            }
        }
    }
}
